#ifndef NIXONBOT_CORE_BNCS_PROTOCOL_CHATEVENT_H_
#define NIXONBOT_CORE_BNCS_PROTOCOL_CHATEVENT_H_

#include <nixonbot/core/bncs/protocol/Message.h>
#include <nixonbot/core/bncs/protocol/MessageFactory.h>

namespace nixonbot {
namespace core {
namespace bncs {
namespace protocol {

struct ChatEventData : public QSharedData
{
  quint32 eventId;
  quint32 userFlags;
  quint32 ping;
  quint32 ipAddress;
  quint32 accountNumber;
  quint32 regAuthority;
  QByteArray user;
  QByteArray text;
};

class ChatEvent : public Message
{
public:
	static const int id = 0x0f;
	BNCS_MESSAGE_SERVER_REGISTER(ChatEvent, id);

public:
	QSharedDataPointer<ChatEventData> d;

public:
	ChatEvent()
	{
		d = new ChatEventData();
	}
	
	virtual ChatEvent* create()
	{
		return new ChatEvent();
	}
	
	virtual quint32 id()
	{
		return id;
	}

	virtual int size()
	{
	  return sizeof(d->eventId) + sizeof(d->userFlags) + sizeof(d->ping) +
	    sizeof(d->ipAddress) + sizeof(d->accountNumber) + sizeof(d->regAuthority) +
	    (d->stat.size() + 1) + (d->accountName.size() + 1);
	}

protected:
	virtual void readFrom(QDataStream& ds)
	{
		int bo = ds.byteOrder();
		ds.setByteOrder(QDataStream::LittleEndian);

		ds >> eventId;
		ds >> userFlags;
		ds >> ping;
		ds >> ipAddress;
		ds >> accountNumber;
		ds >> regAuthority;
		user = readStringFrom(ds);
		text = readStringFrom(ds);

		ds.setByteOrder((QDataStream::ByteOrder)bo);
	}

	virtual void writeTo(QDataStream& ds)
	{
		int bo = ds.byteOrder();
		ds.setByteOrder(QDataStream::LittleEndian);

		ds << eventId;
		ds << userFlags;
		ds << ping;
		ds << ipAddress;
		ds << accountNumber;
		ds << regAuthority;
		writeStringTo(ds, user);
		writeStringTo(ds, text);

		ds.setByteOrder((QDataStream::ByteOrder)bo);
	}
};

}
}
}
}

#endif
